<?php
/**
 * [weliam] Copyright (c) 2016/3/23
 * index.ctrl
 * 全部商品列表控制器
 */
defined('IN_IA') or exit('Access Denied');
$ops = array('test','prompt','door_teacher','door_student');
$op = in_array($_GPC['op'], $ops) ? $_GPC['op'] : '';
//$op = $_GPC['op'];
global $_W;
$rolearray = array('admin','teacher','student');
$rolenamearray =array(
    'admin'=>'管理员',
    'teacher'=>'教师',
    'student'=>'课代表'
);

if($op =='test'){
    global $_W;
    print_r($_W['openid']);
    print_r('测试网页');

}
if($op =='prompt'){
    $prompt = $_GPC['prompt'];
    include wl_template('tips/prompt');
}
if($op =='door_teacher'){
    //判断当前openid的角色为admin，并获得userid
    $current = pdo_get('homework_user', array('uniacid' => $_W['uniacid'] ,'openid'=>$_W['openid']), array('id','role','name'));
    if($current['role']!='admin'){
        $prompt = '您不是管理员，无法使用此功能';
        include wl_template('tips/prompt');
        exit;        
    }
    //获得userid管理的柜子的信息
    $current_cabinet =array();
    $to_current_cabinet = pdo_getall('homework_cabinet_admin', array('uniacid' => $_W['uniacid']));
    foreach($to_current_cabinet as $key=>$value){
        $admins = explode(',' , $value['userid']);
        if(in_array($current['id'],$admins)){
            for($i = 1;$i <= intval($value['door_num']);$i++){
                $current_cabinet[] = array(
                    'device_info_id' => $value['device_info_id'],
                    'device_name' => $value['device_name'],
                    'door' => $i
                );
            }
        }
    }
    //检查该数组，如果为空，则提示信息
    if(count($current_cabinet)==0){
        $prompt = '当前管理员并没有绑定任何柜子！';
        include wl_template('tips/prompt');
        exit;         
    }    
    //当前已经获得管理员管理的柜子的每个柜门的基本信息
    foreach($current_cabinet as $key=>$value){
        $current_door = pdo_get('homework_door_teacher', array('uniacid' => $_W['uniacid'] ,'device_info_id' => $value['device_info_id'] ,'door' => $value['door']), array('userid','time','random'));
        $teacher_name = pdo_get('homework_user', array('uniacid' => $_W['uniacid'] ,'id' => $current_door['userid']), array('name'));
        $current_cabinet[$key]['teacher_name'] = $teacher_name['name'];
        $current_cabinet[$key]['time'] = $current_door['time'];
        $current_cabinet[$key]['random'] = $current_door['random'];
        if($current_door['time']!='0' & $current_door['random']!='')
            $current_cabinet[$key]['isshow'] = true;
        else
            $current_cabinet[$key]['isshow'] = false;
    }
    
     //print_r($current_cabinet);
	include wl_template('generate/door_teacher');
    exit;
}
if($op =='door_student'){
    //判断当前openid的角色为teacher，并获得userid
    $current = pdo_get('homework_user', array('uniacid' => $_W['uniacid'] ,'openid'=>$_W['openid']), array('id','role','name'));
    if($current['role']!='teacher'){
        $prompt = '您不是教师，无法使用此功能';
        include wl_template('tips/prompt');
        exit;        
    }
    //获得信息id与设备名称对应的数组，方便之后使用
    $id_to_name = $this->get_id_to_name();
    //获得该教师管理的柜子的信息，每个柜门只能有一个教师
    $current_cabinet = pdo_getall('homework_door_teacher', array('uniacid' => $_W['uniacid'] ,'userid'=>$current['id']) , array('device_info_id' ,'door'));
    foreach($current_cabinet as $key=>$value){ 
        $current_cabinet[$key]['device_name'] = $id_to_name[$value['device_info_id']];
        
    }

    //检查该数组，如果为空，则提示信息
    if(count($current_cabinet)==0){
        $prompt = '当前教师并没有绑定任何柜门！';
        include wl_template('tips/prompt');
        exit;         
    }    


    //当前已经获得管理员管理的柜子的每个柜门的基本信息,去door_student表找这些柜门对应的学生们有哪些
    //找到time random names
    foreach($current_cabinet as $key=>$value){
        //查找door_student表
        $to_find = pdo_get('homework_door_student', array('uniacid' => $_W['uniacid'] ,'device_info_id' => $value['device_info_id'] ,'door' => $value['door']), array('userid','time','random'));
        if($to_find!=null){
            //不为空，肯定有time random
            $current_cabinet[$key]['random'] = $to_find['random'];
            $current_cabinet[$key]['time'] = $to_find['time'];
            //根据time random判断isshow
            if($to_find['random']!='0'&$to_find['time']!='')
                $current_cabinet[$key]['isshow'] = true;//这里bug，true会变成1
            else
                $current_cabinet[$key]['isshow'] = false;//这里bug，不会进行设置         
            //可能有userid
            if($to_find['userid']!=''){
                $temp_student_s = explode(',' , $to_find['userid']);
                $names ='';  
                $temp_name_array = array();
                foreach($temp_student_s as $k=>$val){//循环获得该柜门对应的学生们的名字
                    $temp_name = pdo_get('homework_user', array('uniacid' => $_W['uniacid'] ,'id' => $val), array('name'));
                    $temp_name_array[] = $temp_name['name'];
                }
                $names = implode(',' , $temp_name_array);
                $current_cabinet[$key]['names'] = $names;                
            }
        }
        

    }
    
    //print_r($current_cabinet);
	include wl_template('generate/door_student');
    exit;
}